//
//  PhotoViewController.m
//  FreeTrip
//
//  Created by 邓芮 on 15/11/26.
//  Copyright © 2015年 邓芮. All rights reserved.
//

#import "PhotoViewController.h"
#import "PhotosModel.h"
//#import "DownloadManagement.h"

@interface PhotoViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIProgressView *progressView;

@property (nonatomic, strong) NSMutableArray *photoArray;

@end

static NSString *cellIdentifier = @"cell";
@implementation PhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
    self.scrollView.delegate = self;
    self.scrollView.backgroundColor = [UIColor colorWithRed:23 / 255.0 green:177 / 255.0 blue:190 / 255.0 alpha:1];
    self.scrollView.contentSize = CGSizeMake(KScreenWidth * self.modelArray.count, KScreenHeight);
    // 当前显示哪张图片
    self.scrollView.contentOffset = CGPointMake(KScreenWidth * self.item, 0);
    
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    
    [self.view addSubview:self.scrollView];
    
//    CGFloat progressWidth = 300 * KScreenWidth / 414;
//    CGFloat progressHeight = 50 * KScreenWidth / 414;
//    self.progressView = [[UIProgressView alloc]initWithFrame:CGRectMake((KScreenWidth - progressWidth) / 2, (KScreenHeight - progressHeight) /2, progressWidth, progressHeight)];
//    self.progressView.trackTintColor = [UIColor orangeColor];
//    self.progressView.progressTintColor = [UIColor purpleColor];
//    self.progressView.progress = 0.5;
//    [self.view addSubview:self.progressView];
    
//    PhotosModel *model = self.modelArray[self.item];
////    Download *download = [[Download alloc]initWithURL:model.image_url];
//    Download *download = [[DownloadManagement shareDownloadManagement]addDownloadWithURL:model.image_url];
//    [download resume];
//    
//    [download Downloading:^(float progress) {
//        self.progressView.progress = progress;
//    } DownloadFinish:^(NSString *savePath) {
//        self.progressView.hidden = YES;
//        [self showViewWithModelItem:self.item];
//        self.scrollView.contentOffset = CGPointMake(KScreenWidth * self.item, KScreenHeight);
//    }];
 
    self.photoArray = [NSMutableArray array];
    for (int i = 0; i < self.modelArray.count; i++)
    {
        UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(KScreenWidth * i, 0, KScreenWidth, KScreenHeight)];
        
//        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//        //button.hidden = YES;
//        CGFloat ww = 40 * KScreenWidth / 414;
//        button.frame = CGRectMake(KScreenWidth - ww - 20 * KScreenWidth / 414, 30 * KScreenWidth / 414, ww, ww);
//        button.tag = 1000 + i;
//        [button setTitle:@"..." forState:UIControlStateNormal];
//        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        button.backgroundColor = [UIColor whiteColor];
//        button.layer.cornerRadius = ww / 2;
//        button.userInteractionEnabled = NO;
//        [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//        [myView addSubview:button];
        [self.scrollView addSubview:myView];
        
        PhotosModel *model = self.modelArray[i];
        
        UIImageView *icon = [[UIImageView alloc]init];
//        [icon sd_setImageWithURL:[NSURL URLWithString:model.image_url]];
//        [icon sd_setImageWithURL:[NSURL URLWithString:model.image_url] placeholderImage:nil options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
//            NSLog(@"%ld", expectedSize);
//        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//            NSLog(@"%@", error);
//        }];
        
        [[SDWebImageDownloader sharedDownloader]downloadImageWithURL:[NSURL URLWithString:model.image_url] options:SDWebImageDownloaderProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
//            self.progressView.progress = receivedSize / 100000;
        } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
            icon.image = image;
            if (finished)
            {
                if (icon.image != nil)
                {
                    icon.userInteractionEnabled = YES;
                    [self.photoArray addObject:icon.image];
                }
                
            }
        }];
        
        CGFloat height = model.image_height * KScreenWidth / model.image_width;
        icon.frame = CGRectMake(0, (KScreenHeight - height)/2, KScreenWidth, height);
        //icon.hidden = YES;
        
//        icon.userInteractionEnabled = YES;
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(click:)];
        [icon addGestureRecognizer:longPress];
        
        
        [myView addSubview:icon];
        
        UILabel *myDescription = [[UILabel alloc]initWithFrame:CGRectMake(0, KScreenHeight, KScreenWidth, 0)];
        if (![model.myDescription isEqualToString:@""] || model.myDescription != nil)
        {
            myDescription.font = [UIFont systemFontOfSize:16 * KScreenWidth / 414];
            myDescription.numberOfLines = 0;
            
            CGRect bounds = [model.myDescription boundingRectWithSize:CGSizeMake(KScreenWidth, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16 * KScreenWidth / 414]} context:nil];
            myDescription.frame = CGRectMake(0, (KScreenHeight - bounds.size.height - 30 * KScreenWidth / 414), KScreenWidth, bounds.size.height + 30 * KScreenWidth / 414);
            
            // myDescription.hidden = YES;
            
            myDescription.textColor = [UIColor blackColor];
            myDescription.backgroundColor = [UIColor colorWithRed:23 / 255.0 green:177 / 255.0 blue:190 / 255.0 alpha:1] ;
            myDescription.text = model.myDescription;
            [myView addSubview:myDescription];
        }

    }
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss:)];
    [self.scrollView addGestureRecognizer:tap];
    
 
    
}

- (void)dismiss:(UITapGestureRecognizer *)tap
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)click:(UILongPressGestureRecognizer *)longPress
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *ensure = [UIAlertAction actionWithTitle:@"保存相册到本地" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"保存照片");
        UIImageView *icon = (UIImageView *)longPress.view;
        UIImage *image = icon.image;
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:ensure];
    [alertController addAction:cancel];
    
    [self presentViewController:alertController animated:YES completion:nil];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
